package zhangzhongyuan.experiment.service;

import zhangzhongyuan.experiment.model.Student;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StudentService {
    private List<Student> students = new ArrayList<>();

    public StudentService() {
        students.add(new Student(1L, "张", "三", "zhangsan@example.com", 20));
        students.add(new Student(2L, "李", "四", "lisi@example.com", 22));
    }

    public List<Student> findAllStudents() {
        return this.students;
    }

    public Student createStudent(Student student) {
        students.add(student);
        return student;
    }
    public Student updateStudent(Long id, Student updatedStudent) {
        for (Student student : students) {
            if (student.getId().equals(id)) {
                student.setFirstName(updatedStudent.getFirstName());
                student.setLastName(updatedStudent.getLastName());
                student.setEmail(updatedStudent.getEmail());
                student.setAge(updatedStudent.getAge());
                return student;
            }
        }
        return null;
    }

    public void deleteStudent(Long id) {
        students.removeIf(student -> student.getId().equals(id));
    }
}
